package com.server.mcp;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.core.McpTool;
import com.server.module.user.domain.User;
import com.server.module.user.service.IUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author WuFengSheng
 * @date 2025/4/19 10:04
 */
@Slf4j
@Service
@AllArgsConstructor
public class McpUserService implements McpTool {

    private final IUserService userService;

    @Tool(description = "根据姓名查询用户信息")
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public String queryUserInfoByName(@ToolParam(description = "姓名") String name) {
        User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getName, name));
        return JSON.toJSONString(user);
    }

    @Tool(description = "根据手机号查询用户信息")
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public String queryUserInfoByPhone(@ToolParam(description = "手机号") String phone) {
        User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, phone));
        return JSON.toJSONString(user);
    }

    @Tool(description = "根据年龄查询用户列表")
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public String queryUserInfoByAge(@ToolParam(description = "年龄") Integer age) {
        Page<User> page = new Page<>(1, 10);
        List<User> userList = userService.list(page, Wrappers.<User>lambdaQuery().eq(User::getAge, age).orderByAsc(User::getName));
        return JSON.toJSONString(userList);
    }

}
